<?php if(in_wechat()){ ?>
<!-- 微信JSSDK -->
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<!-- 微信JSSDK -->
<script type="text/javascript">
wx.config({
    debug: false,
    appId: '{$signPackage.appId}',
    timestamp: {$signPackage.timestamp},
    nonceStr: '{$signPackage.nonceStr}',
    signature: '{$signPackage.signature}',
    jsApiList: ['chooseWXPay']
});

//计算总价运费
function count_transport(area_id,weight,name,tel,address,first_name,third_name){
	
	$.post(
		"{:url('Cart/weixin_update_transport')}",
		{area_id:area_id,weight:weight,name:name,tel:tel,address:address,province:first_name,country:third_name},
		function(d){
			
			if(d.city_id){
				
				$('#city_id').val(d['city_id']);
				
				$('#address_id').val(d['address_id']);	
				
				$('#order_yunfei').text(d['transport']['price']);
				
				t=parseFloat($('#order_amount_sig').text())+parseFloat(d['transport']['price']);  
				
				//$('#order_amount').text(t.toFixed(2));
			}
		}
	);
} 

var o = {};
// localStorage对象
o.Storage = window.localStorage;
 // 收货地址加载标记
window.addressloaded = false;
// order id 生成标记
window.orderId = false;

/**
 * 发起微信支付
 */
$('#wechat-payment-btn').click(wepayCall);

/**
 * 发起微信支付
 * @returns {undefined}
 */
function wepayCall() {

	
    //需要配送的，判断收货地址是否已经获取    
    <?php if($shipping){ ?>
    var address=$('#address_id').val();

    if (address=='') {
        alert('请选择收货地址');
        return false;
    }     
	<?php } ?>
	
	var city_id=$('#city_id').val();
	var address_id=$('#address_id').val();
	var comment=$('#comment').val();

    	
    $('#wechat-payment-btn').addClass('disable').html('支付发起中...');             

  	$.post("{:url('Payment/weixin_pay')}",
  	{
  		city_id:city_id,
		comment:comment,
		address_id:address_id
  	},
  	function (r) {
  		
  		if(r.error){
			$.toast(r.error, "forbidden");
			return;
		}
  		      		
        if (r.ret_code === 0) {
        
          if (r.bizPackage.package !== 'prepay_id=') {
                // 支付操作成功
                r.bizPackage.success = wepayCallback;
                // 支付操作取消
                r.bizPackage.cancel = wepayCancel;
                // 支付操作出错
                r.bizPackage.fail = wepayError;
                // 发起微信支付
                
                wx.chooseWXPay(r.bizPackage);
                // 按钮恢复
                $('#wechat-payment-btn').removeClass('disable').html('微信安全支付');
            } else {
                wepayError();
            }
        } else if(r.ret_code === 11){
            $('#wechat-payment-btn').removeClass('disable').html('微信安全支付');
            alert('订单创建失败！' + r.ret_msg);
        }
    });
      	
    
}
/**
 * 微信支付回调
 * @param {type} res
 * @returns {undefined}
 */
function wepayCallback(res) {
	
	 if(res.errMsg == "chooseWXPay:ok" ) {
        location.href = "{:url('PaySuccess/index')}";
    
    	$('#wechat-payment-btn').removeClass('disable').html('微信安全支付');
        }else{
            alert(res.errMsg);
        }
        
}
   /**
 * 微信支付失败
 */
function wepayError() {
    alert('微信支付发起失败');
    // 按钮恢复
    $('#wechat-payment-btn').removeClass('disable').html('微信安全支付');
    }      
   /**
 * 微信支付手动取消
 */
function wepayCancel() {
    // 按钮恢复
    $('#wechat-payment-btn').removeClass('disable').html('微信安全支付');
}         
    
</script>
	
<?php } ?>